<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <style>
        /* styles.css */
        #sheet-music {
            font-family: 'Arial', sans-serif;
            font-size: 16px;
            line-height: 1.5;
            width: 100%;
            max-width: 600px;
            margin: 0 auto;
        }

        .note {
            color: blue;
        }

        .octave-down {
            color: green;
        }

        .octave-up {
            color: red;
        }

        .rest {
            color: gray;
        }

        .barline {
            font-weight: bold;
        }

        .decoration {
            color: purple;
        }

        .lyrics {
            color: black;
            font-style: italic;
        }

        .lyrics-skip {
            display: none;
        }
    </style>
</head>

<body>
    <div id="sheet-music"></div>
    <script src="../js/jquery.js"></script>
    <script>
        // script.js  
        $(document).ready(function () {
            var sheetMusicData = "CD/E//F//G/A/B/c/|d/e//f//g/a/b>C,/D,//|ef'/G,//A,//B,,-C''|D,2z2|w:这是一段简谱范例。用代码输入*是不是很简单有趣呢？w:This is* a short* exam-ple. Is it* very* easy* and cool to* use *code input";
            var sheetMusicContainer = $('#sheet-music');
            var currentLine = '';
            var inLyrics = false;
            var lyricsText = '';

            // 解析并渲染简谱数据  
            function parseAndRender() {
                sheetMusicData.split('|').forEach(function (section) {
                    var lineElements = [];
                    var sectionArray = section.split('w:');
                    var musicNotes = sectionArray[0].trim();
                    var lyrics = sectionArray[1] ? sectionArray[1].trim() : '';

                    // 处理音符和装饰音  
                    musicNotes.split('').forEach(function (char) {
                        var noteElement;
                        if (char.match(/[CDEFGABcdefgab]/)) { // 基本音符  
                            noteElement = $('<span>').addClass('note').text(char);
                        } else if (char.match(/[',_]/)) { // 八度标记  
                            var prevNote = lineElements[lineElements.length - 1];
                            if (prevNote) {
                                prevNote.text(prevNote.text() + char);
                            }
                        } else if (char.match(/[zZ]/)) { // 休止符  
                            noteElement = $('<span>').addClass('rest').text(char);
                        } else if (char.match(/[{}]/)) { // 装饰音标记（前倚音或后倚音）  
                            var decorationText = '';
                            var nextChar;
                            while ((nextChar = musicNotes.charAt(musicNotes.indexOf(char) + 1)) && !nextChar.match(/[|}]/)) {
                                decorationText += nextChar;
                                musicNotes = musicNotes.replace(nextChar, '');
                            }
                            noteElement = $('<span>').addClass('decoration').text(char + decorationText + (char === '{' ? '}' : ''));
                        } else if (char.match(/[><]/)) { // 附点音符  
                            var prevNote = lineElements[lineElements.length - 1];
                            if (prevNote) {
                                prevNote.text(prevNote.text() + char);
                            }
                        } else if (char.match(/[0-9]/)) { // 音符时值  
                            var prevNote = lineElements[lineElements.length - 1];
                            if (prevNote) {
                                prevNote.text(prevNote.text() + char);
                            }
                        } else if (char.match(/[\/|,]/)) { // 小节线  
                            noteElement = $('<span>').addClass('barline').text(char);
                        } else {
                            // 忽略其他字符或未知标记  
                        }

                        if (noteElement) {
                            lineElements.push(noteElement);
                        }
                    });

                    // 添加歌词  
                    if (lyrics) {
                        inLyrics = true;
                        lyricsText += lyrics + ' ';
                    }

                    // 创建当前行的HTML元素  
                    var lineHtml = $('<div>').append(lineElements);
                    if (inLyrics) {
                        var lyricsSpan = $('<span>').addClass('lyrics').html(lyricsText.replace(/\*/g, '<span class="lyrics-skip"></span>'));
                        lineHtml.append(lyricsSpan);
                        inLyrics = false;
                        lyricsText = '';
                    }
                    sheetMusicContainer.append(lineHtml);
                });
            }

            // 开始解析和渲染  
            parseAndRender();
        });
    </script>
</body>

</html>